﻿using Lawn.NetMessager.WinMessageRec;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
namespace WinMessageRec
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();

         

            this.FormClosing += Form3_FormClosing;

            timer = new System.Threading.Timer(Execute, this, Timeout.Infinite, Timeout.Infinite);
            
        }

        System.Threading.Timer timer;

        private void Form3_FormClosing(object sender, FormClosingEventArgs e)
        {
            timer.Dispose();
            ms?.Close();
        }

        private MessageSender ms = null;
        private void button1_Click(object sender, EventArgs e)
        {

            ms = new MessageSender(int.Parse(txtPort.Text.Trim()));

            // ms.SendMessage(txtTopic.Text, Encoding.UTF8.GetBytes("你好"));

            timer.Change(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1));
            
        }

        private long msg = 1;
        private void Execute(object o)
        {
            ms.SendMessage(txtTopic.Text, Encoding.UTF8.GetBytes($"{this.txtPort.Text}-{msg}"));
            msg++;
        }
    }
}
